package com.situ.blog.controller;



import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.LayUITableResult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/blogType")
public class BlogTypeServlet extends HttpServlet {
    private IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "getBlogTypeListPage":
                getBlogTypeListPage(req, resp);
                break;
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "getBlogTypeAddPage":
                getBlogTypeAddPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
        }
    }

    private void getBlogTypeAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/blogType/blog_type_add.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogTypeServlet.add");
        String name = req.getParameter("name");
        BlogType blogType = new BlogType();
        blogType.setName(name);
        blogTypeService.add(blogType);
        //resp.sendRedirect(req.getContextPath() + "/blogType");

        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogTypeServlet.deleteById");
        String id = req.getParameter("id");
        blogTypeService.deleteById(Integer.parseInt(id));
        //resp.sendRedirect(req.getContextPath() + "/blogType");

        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogTypeServlet.deleteAll");
        //String id = req.getParameter("id");
        String[] ids = req.getParameterValues("ids[]");
        blogTypeService.deleteAll(ids);

        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void getBlogTypeListPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/blogType/blog_type_list.jsp").forward(req, resp);
    }

    //http://localhost:8080/JavaWeb/blogType?method=selectByPage&page=1&limit=10
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BlogTypeServlet.selectByPage");
        String pageStr = req.getParameter("page");
        if (pageStr == null || pageStr.equals("")) {
            pageStr = "1";
        }
        String limitStr = req.getParameter("limit");
        if (limitStr == null || limitStr.equals("")) {
            limitStr = "10";
        }
        int page = Integer.parseInt(pageStr);
        int limit = Integer.parseInt(limitStr);
        String name = req.getParameter("name");
        BlogTypeQuery blogTypeQuery = new BlogTypeQuery(page, limit, name);

        LayUITableResult layUITableResult = blogTypeService.selectByPage(blogTypeQuery);
        System.out.println("LayUITableResult: " + layUITableResult);

        JSONUtil.toJSON(resp, layUITableResult);
    }

}
